package com.huawei.esdk.uc.u19.north.rest.app.resource.ctc;

import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.apache.log4j.Logger;
import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.exception.SDKException;
import com.huawei.esdk.platform.common.utils.ListUtils;
import com.huawei.esdk.platform.common.utils.StringUtils;
import com.huawei.esdk.uc.common.ErrInfo;
import com.huawei.esdk.uc.domain.model.Conference;
import com.huawei.esdk.uc.loggermgr.LoggerMgr;
import com.huawei.esdk.uc.u19.business.professional.rest.ctc.CTCService;
import com.huawei.esdk.uc.u19.north.rest.app.resource.ctc.convert.CTCResourceConvert;
import com.huawei.esdk.uc.u19.north.rest.bean.ctc.EndConf;
import com.huawei.esdk.uc.u19.north.rest.bean.ctc.EndConfResponse;

@Path("uc/u19/del_conf")
public class DelConfResource
{
    private static final Logger LOGGER = Logger.getLogger(DelConfResource.class);
    
    private static final CTCResourceConvert ctcResourceConvert = new CTCResourceConvert();
    
    private static final CTCService ctcService = new CTCService();
    
    /**
     * 终止CTC会议
     * @param confId
     * @return
     */
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public EndConfResponse endConf(EndConf request)
    {
        EndConfResponse response = new EndConfResponse();
        if (null == request || StringUtils.isEmpty(request.getGwIp()) || StringUtils.isEmpty(request.getUserId())
            || ListUtils.isEmptyList(request.getConfRooms()))
        {
            response.setResultCode(String.valueOf(ErrInfo.SDK_UC_PARAM_NOT_COMPLETE_ERRORCODE));
            response.setResultDesc(ErrInfo.SDK_UC_PARAM_NOT_COMPLETE_ERRORCODESC);
            LoggerMgr.interfacelogger("POST", "endConf",
                request, response.getResultCode());
            return response;
        }
        
        // 校验gwIp参数的格式
        if (!StringUtils.isIp(request.getGwIp()))
        {
            response.setResultCode(String.valueOf(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODE));
            response.setResultDesc(String.valueOf(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODE));
            LoggerMgr.interfacelogger("POST", "endConf",
                request, response.getResultCode());
            return response;
        }
   
        try
        {
            List<Integer> confRooms = ctcResourceConvert.endConfRest2Model(request.getConfRooms());
            SDKResult<Conference> result = ctcService.endConf(request.getUserId(), request.getGwIp(), confRooms);
            response = ctcResourceConvert.endConfModel2Rest(result);
        }
        catch (SDKException e)
        {
            LOGGER.error("endConf method SDK error", e);
            response.setResultCode(String.valueOf(e.getSdkErrCode()));
            response.setResultDesc(StringUtils.avoidNull(e.getSdkErrDesc()));
        }
        catch (Exception e)
        {
            LOGGER.error("endConf method error", e);
            response.setResultCode(String.valueOf(ErrInfo.SDK_SYSTEM_ERRORCODE));
            response.setResultDesc(ErrInfo.SDK_SYSTEM_ERRORDESC);
        }
        
        LoggerMgr.interfacelogger("POST", "endConf",
            request, response.getResultCode());
        
        return response;
    }
    
}
